package service;

import dao.RecommendDao;
import model.Page;

import java.sql.SQLException;
import java.util.List;

public class RecommendService {

    RecommendDao rd = new RecommendDao();

    /**
     * 获取指定页数的推荐类别商品通过type
     *
     * @param typeID     type
     * @param pageNumber 第几页
     * @return Page
     * @throws Exception
     */
    public Page selectPageByTypeID(int typeID, int pageNumber) throws Exception {
        Page p = new Page();
        p.setPageNumber(pageNumber);
        int totalCount = 0;
        totalCount = rd.getCountById(typeID);
        p.SetPageSizeAndTotalCount(8, totalCount);
        List list = null;
        list = rd.selectGoodsByTypeID(typeID, pageNumber, 8);
        p.setList(list);
        return p;
    }

    /**
     * 获取推荐商品数通过type
     *
     * @param type type
     * @return int 数量
     */
    public int getCountById(int type) {
        try {
            return rd.getCountById(type);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 判断是否是热销
     *
     * @param goodsId 商品id
     * @return boolean
     */
    public boolean isHot(int goodsId) {
        try {
            return rd.isRecommend(goodsId, 2);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 判断是否是新品
     *
     * @param goodsId 商品id
     * @return boolean
     */
    public boolean isNew(int goodsId) {
        try {
            return rd.isRecommend(goodsId, 3);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 判断是否是条幅
     *
     * @param goodsId 商品id
     * @return boolean
     */
    public boolean isScroll(int goodsId) {
        try {
            return rd.isRecommend(goodsId, 1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean addRecommend(int type, int goodsId) {
        try {
            //查看有没有这个商品id
            boolean flag = new GoodsService().hasGoods(goodsId);
            //查看recommend表里是否已经有了这条数据
            boolean recommend = rd.isRecommend(goodsId, type);
            if (!flag && recommend) return false;
            return rd.addRecommend(type, goodsId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteRecommend(int type, int goodsId) {
        try {
            //查看recommend表里是否已经有了这条数据
            boolean recommend = rd.isRecommend(goodsId, type);
            if (!recommend) return false;
            return rd.deleteRecommend(type, goodsId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
